{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39dc3b36-624a-469b-bf97-7decec05e7e1",
   "metadata": {},
   "source": [
    "## 赛事背景\n",
    "讯飞开放平台针对不同行业、不同场景提供相应的AI能力和解决方案，赋能开发者的产品和应用，帮助开发者通过AI解决相关实际问题，实现让产品能听会说、能看会认、能理解会思考。\n",
    "\n",
    "用户新增预测是分析用户使用场景以及预测用户增长情况的关键步骤，有助于进行后续产品和应用的迭代升级。\n",
    "\n",
    "## 赛事任务\n",
    "本次大赛提供了讯飞开放平台海量的应用数据作为训练样本，参赛选手需要基于提供的样本构建模型，预测用户的新增情况。\n",
    "\n",
    "## 评审规则\n",
    "\n",
    "### 数据说明\n",
    "| 字段名称             | 字段含义                                       |\n",
    "| :--------------- | :----------------------------------------- |\n",
    "| mid              | 用户行为模块id                                   |\n",
    "| eid              | 用户行为事件id                                   |\n",
    "| did              | 用户id                                       |\n",
    "| device\\_brand    | 设备品牌/厂商                                    |\n",
    "| ntt              | 网络类型                                       |\n",
    "| operator         | 运营商                                        |\n",
    "| common\\_country  | 国家                                         |\n",
    "| common\\_province | 省份                                         |\n",
    "| common\\_city     | 城市                                         |\n",
    "| appver           | 应用版本                                       |\n",
    "| channel          | 应用渠道                                       |\n",
    "| common\\_ts       | 事件发生时间（毫秒时间戳）                              |\n",
    "| os\\_type         | 用于判断Android还是iOS                           |\n",
    "| udmap            | 事件自定义属性（标准json文本，内含botId助手ID和pluginId插件ID） |\n",
    "| is\\_new\\_did     | 预测目标，即是否为新增用户                              |\n",
    "\n",
    "### 评估指标\n",
    "本次竞赛的评价标准采用f1_score，分数越高，效果越好。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "0257a336-7cc8-4554-b42d-2d4d3ca0c04c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "import lightgbm as lgb\n",
    "from sklearn.ensemble import HistGradientBoostingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ebd8409b-e0ca-4bd1-9d5d-26003d3c5806",
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.read_csv(\"train.csv\")\n",
    "test = pd.read_csv(\"testA_data.csv\")\n",
    "\n",
    "train[\"common_ts\"] = pd.to_datetime(train[\"common_ts\"], unit=\"ms\")\n",
    "test[\"common_ts\"] = pd.to_datetime(test[\"common_ts\"], unit=\"ms\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "59dbaccb-2f97-42ef-ac3b-18359d1ccc72",
   "metadata": {},
   "outputs": [],
   "source": [
    "train[\"common_month\"] = train[\"common_ts\"].dt.month\n",
    "test[\"common_month\"] = test[\"common_ts\"].dt.month\n",
    "\n",
    "train[\"common_day\"] = train[\"common_ts\"].dt.day\n",
    "test[\"common_day\"] = test[\"common_ts\"].dt.day\n",
    "\n",
    "train[\"common_hour\"] = train[\"common_ts\"].dt.hour\n",
    "test[\"common_hour\"] = test[\"common_ts\"].dt.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "39498021-67f4-489b-8725-48c1dd80f502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3429925, 18), (1143309, 17))"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape, test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e9a45a51-11af-452a-99c1-947206be2d60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['mid', 'eid', 'did', 'device_brand', 'ntt', 'operator',\n",
       "       'common_country', 'common_province', 'common_city', 'appver', 'channel',\n",
       "       'common_ts', 'os_type', 'udmap', 'is_new_did', 'common_month',\n",
       "       'common_day', 'common_hour'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c294feb9-8aed-45d9-a4e7-76a52e817caa",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/dl/0x0qfc0s6vd9wdk53j4thvnh0000gn/T/ipykernel_9042/39591749.py:1: FutureWarning: Treating datetime data as categorical rather than numeric in `.describe` is deprecated and will be removed in a future version of pandas. Specify `datetime_is_numeric=True` to silence this warning and adopt the future behavior now.\n",
      "  train.describe(include=\"all\")\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mid</th>\n",
       "      <th>eid</th>\n",
       "      <th>did</th>\n",
       "      <th>device_brand</th>\n",
       "      <th>ntt</th>\n",
       "      <th>operator</th>\n",
       "      <th>common_country</th>\n",
       "      <th>common_province</th>\n",
       "      <th>common_city</th>\n",
       "      <th>appver</th>\n",
       "      <th>channel</th>\n",
       "      <th>common_ts</th>\n",
       "      <th>os_type</th>\n",
       "      <th>udmap</th>\n",
       "      <th>is_new_did</th>\n",
       "      <th>common_month</th>\n",
       "      <th>common_day</th>\n",
       "      <th>common_hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3429925</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3429925</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3429925</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "      <td>3.429925e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>270837</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3254416</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8077</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20cd6a7d3a60fd193d925b21af6660f1e</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2025-03-13 00:05:47.273000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>{}</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>68403</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>41</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3162776</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2025-02-28 16:00:00.115000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>last</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2025-03-31 15:59:57.196000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2.264608e+01</td>\n",
       "      <td>1.366922e+02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.892087e+01</td>\n",
       "      <td>2.605180e+00</td>\n",
       "      <td>1.929892e+00</td>\n",
       "      <td>8.091360e+01</td>\n",
       "      <td>1.459701e+02</td>\n",
       "      <td>2.400957e+02</td>\n",
       "      <td>5.874532e+01</td>\n",
       "      <td>5.914274e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.228171e-01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.560340e-01</td>\n",
       "      <td>2.997912e+00</td>\n",
       "      <td>1.578363e+01</td>\n",
       "      <td>8.535301e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.393127e+01</td>\n",
       "      <td>7.687001e+01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.289133e+01</td>\n",
       "      <td>1.148252e+00</td>\n",
       "      <td>1.140171e+00</td>\n",
       "      <td>2.438861e+00</td>\n",
       "      <td>7.985908e+01</td>\n",
       "      <td>1.414514e+02</td>\n",
       "      <td>2.818215e+01</td>\n",
       "      <td>4.161772e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.846814e-01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.628876e-01</td>\n",
       "      <td>4.564792e-02</td>\n",
       "      <td>8.624008e+00</td>\n",
       "      <td>5.391190e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>2.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.300000e+01</td>\n",
       "      <td>6.400000e+01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.800000e+01</td>\n",
       "      <td>2.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>8.100000e+01</td>\n",
       "      <td>7.900000e+01</td>\n",
       "      <td>8.900000e+01</td>\n",
       "      <td>2.600000e+01</td>\n",
       "      <td>2.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>8.000000e+00</td>\n",
       "      <td>4.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.100000e+01</td>\n",
       "      <td>1.190000e+02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.500000e+01</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>2.000000e+00</td>\n",
       "      <td>8.100000e+01</td>\n",
       "      <td>1.650000e+02</td>\n",
       "      <td>2.660000e+02</td>\n",
       "      <td>6.900000e+01</td>\n",
       "      <td>5.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>1.600000e+01</td>\n",
       "      <td>8.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>3.000000e+01</td>\n",
       "      <td>2.270000e+02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.530000e+02</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>8.100000e+01</td>\n",
       "      <td>2.010000e+02</td>\n",
       "      <td>3.660000e+02</td>\n",
       "      <td>8.300000e+01</td>\n",
       "      <td>1.100000e+01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>2.300000e+01</td>\n",
       "      <td>1.200000e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>5.600000e+01</td>\n",
       "      <td>2.550000e+02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.140000e+02</td>\n",
       "      <td>5.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>1.110000e+02</td>\n",
       "      <td>2.790000e+02</td>\n",
       "      <td>4.570000e+02</td>\n",
       "      <td>1.070000e+02</td>\n",
       "      <td>1.700000e+01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>3.100000e+01</td>\n",
       "      <td>2.300000e+01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 mid           eid                                did  \\\n",
       "count   3.429925e+06  3.429925e+06                            3429925   \n",
       "unique           NaN           NaN                             270837   \n",
       "top              NaN           NaN  20cd6a7d3a60fd193d925b21af6660f1e   \n",
       "freq             NaN           NaN                              68403   \n",
       "first            NaN           NaN                                NaN   \n",
       "last             NaN           NaN                                NaN   \n",
       "mean    2.264608e+01  1.366922e+02                                NaN   \n",
       "std     1.393127e+01  7.687001e+01                                NaN   \n",
       "min     0.000000e+00  0.000000e+00                                NaN   \n",
       "25%     1.300000e+01  6.400000e+01                                NaN   \n",
       "50%     2.100000e+01  1.190000e+02                                NaN   \n",
       "75%     3.000000e+01  2.270000e+02                                NaN   \n",
       "max     5.600000e+01  2.550000e+02                                NaN   \n",
       "\n",
       "        device_brand           ntt      operator  common_country  \\\n",
       "count   3.429925e+06  3.429925e+06  3.429925e+06    3.429925e+06   \n",
       "unique           NaN           NaN           NaN             NaN   \n",
       "top              NaN           NaN           NaN             NaN   \n",
       "freq             NaN           NaN           NaN             NaN   \n",
       "first            NaN           NaN           NaN             NaN   \n",
       "last             NaN           NaN           NaN             NaN   \n",
       "mean    8.892087e+01  2.605180e+00  1.929892e+00    8.091360e+01   \n",
       "std     5.289133e+01  1.148252e+00  1.140171e+00    2.438861e+00   \n",
       "min     0.000000e+00  0.000000e+00  0.000000e+00    1.000000e+00   \n",
       "25%     5.800000e+01  2.000000e+00  1.000000e+00    8.100000e+01   \n",
       "50%     6.500000e+01  3.000000e+00  2.000000e+00    8.100000e+01   \n",
       "75%     1.530000e+02  3.000000e+00  3.000000e+00    8.100000e+01   \n",
       "max     2.140000e+02  5.000000e+00  3.000000e+00    1.110000e+02   \n",
       "\n",
       "        common_province   common_city        appver       channel  \\\n",
       "count      3.429925e+06  3.429925e+06  3.429925e+06  3.429925e+06   \n",
       "unique              NaN           NaN           NaN           NaN   \n",
       "top                 NaN           NaN           NaN           NaN   \n",
       "freq                NaN           NaN           NaN           NaN   \n",
       "first               NaN           NaN           NaN           NaN   \n",
       "last                NaN           NaN           NaN           NaN   \n",
       "mean       1.459701e+02  2.400957e+02  5.874532e+01  5.914274e+00   \n",
       "std        7.985908e+01  1.414514e+02  2.818215e+01  4.161772e+00   \n",
       "min        0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "25%        7.900000e+01  8.900000e+01  2.600000e+01  2.000000e+00   \n",
       "50%        1.650000e+02  2.660000e+02  6.900000e+01  5.000000e+00   \n",
       "75%        2.010000e+02  3.660000e+02  8.300000e+01  1.100000e+01   \n",
       "max        2.790000e+02  4.570000e+02  1.070000e+02  1.700000e+01   \n",
       "\n",
       "                         common_ts       os_type    udmap    is_new_did  \\\n",
       "count                      3429925  3.429925e+06  3429925  3.429925e+06   \n",
       "unique                     3254416           NaN     8077           NaN   \n",
       "top     2025-03-13 00:05:47.273000           NaN       {}           NaN   \n",
       "freq                            41           NaN  3162776           NaN   \n",
       "first   2025-02-28 16:00:00.115000           NaN      NaN           NaN   \n",
       "last    2025-03-31 15:59:57.196000           NaN      NaN           NaN   \n",
       "mean                           NaN  6.228171e-01      NaN  1.560340e-01   \n",
       "std                            NaN  4.846814e-01      NaN  3.628876e-01   \n",
       "min                            NaN  0.000000e+00      NaN  0.000000e+00   \n",
       "25%                            NaN  0.000000e+00      NaN  0.000000e+00   \n",
       "50%                            NaN  1.000000e+00      NaN  0.000000e+00   \n",
       "75%                            NaN  1.000000e+00      NaN  0.000000e+00   \n",
       "max                            NaN  1.000000e+00      NaN  1.000000e+00   \n",
       "\n",
       "        common_month    common_day   common_hour  \n",
       "count   3.429925e+06  3.429925e+06  3.429925e+06  \n",
       "unique           NaN           NaN           NaN  \n",
       "top              NaN           NaN           NaN  \n",
       "freq             NaN           NaN           NaN  \n",
       "first            NaN           NaN           NaN  \n",
       "last             NaN           NaN           NaN  \n",
       "mean    2.997912e+00  1.578363e+01  8.535301e+00  \n",
       "std     4.564792e-02  8.624008e+00  5.391190e+00  \n",
       "min     2.000000e+00  1.000000e+00  0.000000e+00  \n",
       "25%     3.000000e+00  8.000000e+00  4.000000e+00  \n",
       "50%     3.000000e+00  1.600000e+01  8.000000e+00  \n",
       "75%     3.000000e+00  2.300000e+01  1.200000e+01  \n",
       "max     3.000000e+00  3.100000e+01  2.300000e+01  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.describe(include=\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0f268659-d979-4dd4-a232-c07f631830c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for col in ['mid', 'eid', 'did', 'device_brand', 'ntt', 'operator',\n",
    "       'common_country', 'common_province', 'common_city', 'appver', 'channel',\n",
    "       'os_type']:\n",
    "    train[col + \"_count\"] = train[col].map(train[col].value_counts())\n",
    "    test[col + \"_count\"] = test[col].map(test[col].value_counts())\n",
    "\n",
    "    train[col + \"_target\"] = train[col].map(train.groupby(col)[\"is_new_did\"].mean())\n",
    "    test[col + \"_target\"] = test[col].map(train.groupby(col)[\"is_new_did\"].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e2c9afa5-3c0e-48c1-a9ec-3cec193f4745",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9050957798130362"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred = cross_val_predict(\n",
    "    HistGradientBoostingClassifier(),\n",
    "    train.drop([\"did\", \"udmap\", \"is_new_did\", \"common_ts\"], axis=1),\n",
    "    train[\"is_new_did\"]\n",
    ")\n",
    "f1_score(train[\"is_new_did\"], pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a3a6c05c-fb9b-42d0-b511-d3529c45e80b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Info] Number of positive: 535185, number of negative: 2894740\n",
      "[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.039179 seconds.\n",
      "You can set `force_row_wise=true` to remove the overhead.\n",
      "And if memory is not enough, you can set `force_col_wise=true`.\n",
      "[LightGBM] [Info] Total Bins 3023\n",
      "[LightGBM] [Info] Number of data points in the train set: 3429925, number of used features: 38\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.156034 -> initscore=-1.688038\n",
      "[LightGBM] [Info] Start training from score -1.688038\n"
     ]
    }
   ],
   "source": [
    "model = lgb.LGBMClassifier()\n",
    "model.fit(\n",
    "    train.drop([\"did\", \"udmap\", \"is_new_did\", \"common_ts\"], axis=1),\n",
    "    train[\"is_new_did\"]\n",
    ")\n",
    "pred = model.predict(test.drop([\"did\", \"udmap\", \"common_ts\"], axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1ce1d7db-4079-4817-9cda-c2b4e35100ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame({\"is_new_did\": pred}).to_csv(\"submit.csv\", index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f5c1055-decb-4ac7-b6de-e71d87eb7d72",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
